package com.example.smartcity_poverty.ui.AllService.YouthPost.activity

import android.content.Context
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.MenuItem
import com.example.smartcity_poverty.App.Companion.url
import com.example.smartcity_poverty.R
import com.example.smartcity_poverty.logic.network.AppApi
import com.example.smartcity_poverty.logic.network.ServiceNET.Companion.create
import com.example.smartcity_poverty.logic.util.Util.glide
import com.youth.banner.adapter.BannerImageAdapter
import com.youth.banner.holder.BannerImageHolder
import com.youth.banner.indicator.CircleIndicator
import kotlinx.android.synthetic.main.activity_yh_show_list.*
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch

class YhShowListActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        supportActionBar?.setDisplayHomeAsUpEnabled(true)
        setContentView(R.layout.activity_yh_show_list)
        title = "驿站详情"

        val id = intent.getStringExtra("id")?.toInt()
        // 填充数据
        id?.let {
            setIdShow(id)
        }
    }

    private fun setIdShow(id: Int) {
        // 存储驿站照片
        val arr = ArrayList<String>()

        GlobalScope.launch(Dispatchers.Main){
            create<AppApi>().getYhUserListAddAll(
                getSharedPreferences("token", Context.MODE_PRIVATE).getString("token", "").toString(),
                id
            ).apply {
                if (this.code == 200){
                    this.data?.let {

                        // 分割字符串
                        val a = it.imageUrlList.toString().split(",","[","]"," "," ")
                        for (i in a){
                            if (i.isNotEmpty()){
                                Log.d("Banner", "setIdShow: --->$url$i")
                                arr.add(url+i)
                            }
                        }

                        // 轮播图设置
                        yhShowBanner.apply {
                            this.adapter = object : BannerImageAdapter<String>(arr){
                                override fun onBindView(p0: BannerImageHolder?, p1: String?, p2: Int, p3: Int) {
                                    glide(p1.toString(), p0?.imageView!!)
                                }
                            }
                            this.addBannerLifecycleObserver(this@YhShowListActivity).indicator = CircleIndicator(context)
                            this.setBannerRound(25f)
                        }


                        yhShowThis.text = "驿站地点: ${it.address}"
                        yhShowPhone.text = "电话: ${it.phone}"
                        yhShowTime.text = "工作办理入住时间: ${it.workTime}"
                        yhShowOne.text = "剩余男床位: ${it.bedsCountBoy}"
                        yhShowTwo.text = "剩余女床位: ${it.bedsCountGirl}"

                        yhShow1.text = "公寓介绍: ${it.introduce}"
                        yhShow2.text = "内部配置: ${it.internalFacilities}"
                        yhShow3.text = "周边配套: ${it.surroundingFacilities}"
                        yhShow4.text = "特色服务: ${it.specialService}"
                    }
                }
            }
        }
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        finish()
        return super.onOptionsItemSelected(item)
    }
}